﻿package mortal.game.manager.mouse
{
    import com.gengine.core.frame.*;
    import com.gengine.global.*;
    import flash.events.*;
    import flash.utils.*;
    import frEngine.event.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.events.*;
    import mortal.game.scene3D.layer3D.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;

    public class MouseLongPress extends Object
    {
        protected var _holdTime:Number = 300;
        protected var _aryIntervalAction:Array;
        protected var _lastDownTime:Number;
        protected var _startEntity:MovePlayer;
        protected var _isBreakDown:Boolean = false;
        protected var _timeOut:int = 0;
        protected var _frameTimer:FrameTimer;
        protected var _attackPlayer:MovePlayer;
        static var _instance:MouseLongPress;

        public function MouseLongPress(param1:Number)
        {
            this._aryIntervalAction = new Array();
            this._holdTime = param1;
            return;
        }// end function

        public function reset(param1:Number) : void
        {
            this.dispose();
            this._holdTime = param1;
            return;
        }// end function

        public function setMouseDown() : void
        {
            if (ThingUtil.overEntity && ThingUtil.overEntity is MovePlayer)
            {
                this._startEntity = ThingUtil.overEntity as MovePlayer;
            }
            this._lastDownTime = getTimer();
            this._isBreakDown = false;
            with ({})
            {
                {}.action = function () : void
            {
                if (!_isBreakDown)
                {
                    startLongMode();
                }
                return;
            }// end function
            ;
            }
            this._timeOut = setTimeout(function () : void
            {
                if (!_isBreakDown)
                {
                    startLongMode();
                }
                return;
            }// end function
            , this._holdTime);
            this.addUpEvent();
            return;
        }// end function

        protected function addUpEvent() : void
        {
            Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.onTargetMouseUp);
            return;
        }// end function

        protected function removeUpEvent() : void
        {
            Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onTargetMouseUp);
            return;
        }// end function

        protected function onTargetMouseUp(event:MouseEvent) : void
        {
            this._isBreakDown = true;
            this.stopLongMode();
            clearTimeout(this._timeOut);
            this.removeUpEvent();
            return;
        }// end function

        public function get frameTimer() : FrameTimer
        {
            if (!this._frameTimer)
            {
                this._frameTimer = new FrameTimer(5);
            }
            return this._frameTimer;
        }// end function

        protected function startLongMode() : void
        {
            this.attackProcess(this._startEntity);
            return;
        }// end function

        public function stopLongMode() : void
        {
            if (this._frameTimer)
            {
                this.frameTimer.stop();
                this.frameTimer.reset();
            }
            GameController.shortcut.keyProcessor.mouseDownUseCombat = false;
            this.disposeAttackEvent();
            this._startEntity = null;
            this.removeUpEvent();
            return;
        }// end function

        public function attackProcess(param1 = null) : void
        {
            this.disposeAttackEvent();
            var _loc_2:Boolean = false;
            if (param1 is MovePlayer && this.attack(param1))
            {
                return;
            }
            if (ThingUtil.overEntity && ThingUtil.overEntity is MovePlayer && this.attack(ThingUtil.overEntity as MovePlayer))
            {
                return;
            }
            GameController.shortcut.keyProcessor.mouseDownUseCombat = false;
            this.moveProcess();
            return;
        }// end function

        protected function attack(param1:MovePlayer) : Boolean
        {
            if (EntityDictionary.isEntityFit(param1, null, EntityRelationUtil.ENEMY, false, param1.selected))
            {
                ThingUtil.selectEntity = ThingUtil.overEntity as MovePlayer;
                this._attackPlayer = param1 as MovePlayer;
                GameController.shortcut.keyProcessor.mouseDownUseCombat = true;
                FrEventDispatcher.instance.proxyAddEventListener(this._attackPlayer, PlayerEvent.ENTITY_DEAD, this.attackProcess, false);
                return true;
            }
            return false;
        }// end function

        protected function moveProcess() : void
        {
            if (this._frameTimer && this._frameTimer.running)
            {
                return;
            }
            this.frameTimer.addListener(TimerType.ENTERFRAME, this.onFrameTimer);
            this.frameTimer.start();
            return;
        }// end function

        public function onFrameTimer(param1:FrameTimer) : void
        {
            var _loc_3:IntervalAction = null;
            var _loc_2:* = this.frameTimer.currentCount;
            for each (_loc_3 in this._aryIntervalAction)
            {
                
                if ((_loc_2 - 1) % _loc_3.interval == 0)
                {
                    _loc_3.action.call();
                }
            }
            return;
        }// end function

        public function setIntervalAction(param1:int, param2:Function) : void
        {
            this._aryIntervalAction.push(new IntervalAction(param1, param2));
            return;
        }// end function

        protected function disposeAttackEvent() : void
        {
            if (this._attackPlayer)
            {
                FrEventDispatcher.instance.proxyRemoveEventListener(this._attackPlayer, PlayerEvent.ENTITY_DEAD, this.attackProcess, false);
                this._attackPlayer = null;
            }
            return;
        }// end function

        public function dispose() : void
        {
            this.stopLongMode();
            this._aryIntervalAction = [];
            return;
        }// end function

        public static function get instance() : MouseLongPress
        {
            if (!_instance)
            {
                _instance = new MouseLongPress(300);
            }
            return _instance;
        }// end function

    }
}

class IntervalAction extends Object
{
    public var interval:Number;
    public var action:Function;

    function IntervalAction(param1:Number, param2:Function)
    {
        this.interval = param1;
        this.action = param2;
        return;
    }// end function

}

